import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:multi_show/model/take_photo_result.dart';

class MultiShow {
  static const MethodChannel _channel =
      const MethodChannel('multi_show');

  static Future<String> get platformVersion async {
    final String version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }
  static Future<void> multiShow(List<String> paths,num currentIndex) async {
    final arguments = {
      "paths":paths,
      "currentIndex":currentIndex,
    };
    await _channel.invokeMethod('multiShow',arguments);
  }
  /*
  * 选择 图片 / 视频
  * int maxCoshount 最大资源数量
  * int selectType 资源类型  1：图片  2：视频  3：图片+视频
  * */
  static Future<TakePhotoResult> takePhoto({int maxCoshount,int selectType}) async {
    final arguments = {
      "maxCoshount":maxCoshount,
      "selectType":selectType,
    };
    try {
      final resultMap = await _channel.invokeMethod('takePhoto',arguments);
      final map = new Map<String, dynamic>.from(resultMap);
      final result = TakePhotoResult.fromJson(map);
      return result;
    } catch (e) {
      throw e;
    }
  }

  /*
  * 获取本地视频的预览图
  * @required String videoPath 视频地址
  * */
  static Future<String> getThumbPathForAssetVideo({@required String videoPath}) async {
    final arguments = {
      "videoPath":videoPath,
    };
    try {
      final resultMap = await _channel.invokeMethod('thumbPathForAssetVideo',arguments);
      final map = new Map<String, dynamic>.from(resultMap);
      final success = map["success"];
      final path = map["path"] as String;
      final error = map["error"];
      if (!success || path == null || path.length <= 0) {
        throw Future.error(error);
      }
      return path;
    } catch (e) {
      throw e;
    }
  }

  /*
  * 获取网络视频的预览图
  * @required String videoPath 视频地址
  * */
  static Future<String> getThumbPathForNetworkVideo({@required String videoPath}) async {
    final arguments = {
      "videoPath":videoPath,
    };
    try {
      final resultMap = await _channel.invokeMethod('thumbPathForNetworkVideo',arguments);
      final map = new Map<String, dynamic>.from(resultMap);
      final success = map["success"];
      final path = map["path"] as String;
      final error = map["error"];
      if (!success || path == null || path.length <= 0) {
        throw Future.error(error);
      }
      return path;
    } catch (e) {
      throw e;
    }
  }
}
